#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDialog>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    on_actionTile_triggered();
    ui->subwindow->setWindowTitle("主窗口");
    //setWindowFlags(windowFlags() & ~Qt::WindowMinMaxButtonsHint);

    //本文链接：https://blog.csdn.net/weixin_52919504/article/details/133172808
//    ui->toolButton->setPopupMode(QToolButton::DelayedPopup);
//    ui->toolButton->setPopupMode(QToolButton::MenuButtonPopup);
    //1 将toolButton添加到工具栏
    ui->toolBar->addWidget(ui->toolButton);

    ui->toolButton->setPopupMode(QToolButton::InstantPopup);
    // 创建 QMenu
    QMenu *menu = new QMenu(ui->toolButton);

    // 添加 QAction 到 QMenu
    QAction *action1 = menu->addAction("窗口 1");
    QAction *action2 = menu->addAction("窗口 2");
    QAction *action3 = menu->addAction("窗口 3");
    QAction *action4 = menu->addAction("窗口 4");

    // 连接 QAction 的 triggered 信号到槽函数
    connect(action1, SIGNAL(triggered(bool)), this, SLOT(onActionTriggered(bool)));
    connect(action2, &QAction::triggered, this, &MainWindow::onActionTriggered);
    connect(action3, &QAction::triggered, this, &MainWindow::onActionTriggered);
    connect(action4, SIGNAL(triggered(bool)), this, SLOT(onActionTriggered(bool)));

    // 将 QMenu 设置为 QToolButton 的下拉菜单
    ui->toolButton->setMenu(menu);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onActionTriggered(bool checked)
{
    QAction *action = qobject_cast<QAction *>(sender());

    if (action)
    {
        qDebug() << "Selected:" << action->text() << "checked" << checked;
        if(action->text()=="窗口 1")      CreatSubWindow(1);
        else if(action->text()=="窗口 2") CreatSubWindow(2);
        else if(action->text()=="窗口 3") CreatSubWindow(3);
        else if(action->text()=="窗口 4") CreatSubWindow(4);
    }
}

/*
void MainWindow::on_actionOpen_triggered()
{
    // 创建Dialog窗体
    QDialog *formDoc = new QDialog(this);

    formDoc->setWindowTitle("1");

    //formDoc->windowTitle();
    // 文档窗口添加到MDI
    ui->mdiArea->addSubWindow(formDoc);

    // 在单独的窗口中显示
    formDoc->show();
}
*/

void MainWindow::on_actionOpen_triggered()
{
    if(subwindowNum >= MaxSubWindowNum) return;

    // 创建Dialog窗体
    subWindow[subwindowNum] = new QDialog(this);

    subWindow[subwindowNum]->setWindowTitle("新建子窗口"+QString::number(subwindowNum));

    QLabel *label = new QLabel(subWindow[subwindowNum]); // 'this' 是当前窗口或容器的指针
    QString Picture = ":/res/";
    Picture += QString::number(subwindowNum%10) + ".jpg";

    QPixmap pixmap(Picture); // 替换为您的图片路径
    label->setPixmap(pixmap);
    // 设置QLabel的大小（可选）
    //label->setGeometry(QRect(QPoint(10, 10), pixmap.size()));

    // 文档窗口添加到MDI
    ui->mdiArea->addSubWindow(subWindow[subwindowNum]);

    // 在单独的窗口中显示
    subWindow[subwindowNum]->show();
    subwindowNum++;
}

void MainWindow::CreatSubWindow(int Num)
{
    if(Num >= MaxSubWindowNum) return;

    on_actionClose_triggered();

    for(int i=0; i<Num; i++)
    {
        // 创建Dialog窗体
        subWindow[subwindowNum] = new QDialog(this);

        subWindow[subwindowNum]->setWindowTitle("新建子窗口"+QString::number(subwindowNum));

        QLabel *label = new QLabel(subWindow[subwindowNum]); // 'this' 是当前窗口或容器的指针
        QString Picture = ":/res/";
        Picture += QString::number(subwindowNum%10) + ".jpg";

        QPixmap pixmap(Picture); // 替换为您的图片路径
        label->setPixmap(pixmap);
        // 设置QLabel的大小（可选）
        //label->setGeometry(QRect(QPoint(10, 10), pixmap.size()));

        // 文档窗口添加到MDI
        ui->mdiArea->addSubWindow(subWindow[subwindowNum]);

        // 在单独的窗口中显示
        subWindow[subwindowNum]->show();
        subwindowNum++;
    }

    ui->mdiArea->tileSubWindows();
}

void MainWindow::on_actionClose_triggered()
{
    // 关闭所有子窗口
    ui->mdiArea->closeAllSubWindows();
    subwindowNum = 0;
}

void MainWindow::on_actionMID_toggled(bool arg1)
{
    // Tab多页显示模式
    if (arg1)
    {
        // Tab多页显示模式
        ui->mdiArea->setViewMode(QMdiArea::TabbedView);
        // 页面可关闭
        ui->mdiArea->setTabsClosable(true);
        ui->actionLine->setEnabled(false);
        ui->actionTile->setEnabled(false);
    }
    // 子窗口模式
    else
    {
        // 子窗口模式
        ui->mdiArea->setViewMode(QMdiArea::SubWindowView);
        ui->actionLine->setEnabled(true);
        ui->actionTile->setEnabled(true);
    }
}

void MainWindow::on_actionLine_triggered()
{
    ui->mdiArea->cascadeSubWindows();
}

void MainWindow::on_actionTile_triggered()
{
    ui->mdiArea->tileSubWindows();
}

void MainWindow::on_actionWindow_triggered()
{
    ui->mdiArea->setViewMode(QMdiArea::SubWindowView);
    ui->actionLine->setEnabled(true);
    ui->actionMID->setEnabled(true);
    ui->actionTile->setEnabled(true);
}

void MainWindow::on_pushButton_clicked()
{
    /*
    qDebug() << "x: " << this->geometry().x() << "y: " << this->geometry().y();
    qDebug() << "width: " << this->geometry().width() << "height: " << this->geometry().height();

    // 获取子窗口的位置
    QPoint subwindowPositionInMainWindow = ui->subwindow->mapToGlobal(QPoint(0, 0)); //屏幕左上角
    int x = subwindowPositionInMainWindow.x()-geometry().x();
    int y = subwindowPositionInMainWindow.y()-geometry().y();

    // 获取子窗口的大小
    QSize subWindowSize = ui->subwindow->size();
    int width = subWindowSize.width();
    int height = subWindowSize.height();

    // 打印这些信息
    qDebug() << "Subwindow Position:" << x << "," << y;
    qDebug() << "Subwindow Size:" << width << "x" << height;
    */

    // 打印这些信息
    qDebug() << "Subwindow x,y : " << ui->subwindow->mapToGlobal(QPoint(0, 0)).x()-geometry().x()
                            << "," << ui->subwindow->mapToGlobal(QPoint(0, 0)).y()-geometry().y();
    qDebug() << "Subwindow Size: " << ui->subwindow->size().width()
                            << "x" << ui->subwindow->size().height();

    // 打印这些信息
    qDebug() << "Subwindow2 x,y : " << ui->subwindow_2->mapToGlobal(QPoint(0, 0)).x()-geometry().x()\
                            << "," << ui->subwindow_2->mapToGlobal(QPoint(0, 0)).y()-geometry().y();
    qDebug() << "Subwindow2 Size: " << ui->subwindow_2->size().width()
                            << "x" << ui->subwindow_2->size().height();

    // 打印这些信息
    qDebug() << "Subwindow3 x,y : " << ui->subwindow_3->mapToGlobal(QPoint(0, 0)).x()-geometry().x()\
                            << "," << ui->subwindow_3->mapToGlobal(QPoint(0, 0)).y()-geometry().y();
    qDebug() << "Subwindow3 Size: " << ui->subwindow_3->size().width()
                            << "x" << ui->subwindow_3->size().height();

    // 打印这些信息
    qDebug() << "Subwindow4 x,y : " << ui->subwindow_4->mapToGlobal(QPoint(0, 0)).x()-geometry().x()\
                            << "," << ui->subwindow_4->mapToGlobal(QPoint(0, 0)).y()-geometry().y();
    qDebug() << "Subwindow4 Size: " << ui->subwindow_4->size().width()
                            << "x" << ui->subwindow_4->size().height();
}

//#include <QWidget>
void MainWindow::on_pushButton_2_clicked()
{
    // 获取 QMdiArea 中的所有子窗口列表
    QList<QMdiSubWindow *> subWindows = ui->mdiArea->subWindowList();

    // 获取子窗口的数量
    int numSubWindows = subWindows.size();

    // 打印子窗口数量
    qDebug() << "Number of subwindows:" << numSubWindows;

    if(subWindow[0]!=NULL )
    {
        qDebug() << subWindow[0]->windowTitle();
        // 打印这些信息
        qDebug() << "formDoc[0] x,y : " << subWindow[0]->mapToGlobal(QPoint(0, 0)).x()-geometry().x()\
                                 << "," << subWindow[0]->mapToGlobal(QPoint(0, 0)).y()-geometry().y();
        qDebug() << "formDoc[0] Size: " << subWindow[0]->size().width()
                                 << "x" << subWindow[0]->size().height();
    }
}
